package com.css.fxfzcpxx.modules.hddcMap.service.impl;

import com.css.fxfzcpxx.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzcpxx.base.attachment.service.AttachmentInfoService;
import com.css.fxfzcpxx.constants.FxfzConstants;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.hddcMap.entity.DrawingEntity;
import com.css.fxfzcpxx.modules.hddcMap.repository.HddcMapRepository;
import com.css.fxfzcpxx.modules.hddcMap.service.HddcMapService;
import com.css.fxfzcpxx.modules.hddcMap.vo.UpdateAttachVO;
import com.css.fxfzcpxx.modules.informationAchievement.entity.InfAchievement;
import com.css.fxfzcpxx.modules.thematicMap.vo.OtherSystemInfoVo;
import com.css.fxfzcpxx.modules.thematicMap.entity.ThematicMap;
import com.css.fxfzcpxx.modules.thematicMap.service.TheMapService;
import com.css.fxfzcpxx.util.PlatformDateUtils;
import com.css.fxfzcpxx.util.PlatformSessionUtils;
import com.css.fxfzcpxx.util.StringUtils;
import com.css.fxfzcpxx.util.UUIDGenerator;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author y1fan
 * @description
 */
@Service
public class HddcMapServiceImpl implements HddcMapService {


    @Resource
    private HddcMapRepository hddcMapRepository;

    @Value("${image.localDir}")
    private String localDir;
    @Resource
    AttachmentInfoService attachmentInfoService;
    @Resource
    private TheMapService theMapService;

    @Override
    public void save(OtherSystemInfoVo otherSystemInfoVo) throws IOException {
        MultipartFile file = otherSystemInfoVo.getFile();
        //获取文件名称
        String fileName = file.getOriginalFilename();
        //获取文件上传的本地路径
        String dateDir = new SimpleDateFormat("/yyyy/MM/dd").format(new Date());
        String savePath = localDir + dateDir;
        //构建文件
        assert fileName != null;
        //上传文件
        String realFileName = uploadFile(fileName, savePath, file);
        //构建实体类
        AttachmentInfoEntity attachmentInfoEntity = buildAttachment(fileName, dateDir, realFileName);
        //保存附件到数据库
        int flag = 0;
        if (ObjectUtils.isNotEmpty(attachmentInfoEntity)) {
            flag = attachmentInfoService.singleSave(attachmentInfoEntity);
        }
        ThematicMap thematicMap=new ThematicMap();
        if (flag == 1) {
            otherSystemInfoVo.setReleaseStatus(YNEnum.Y.toString());
            otherSystemInfoVo.setPlotTime(new Date());
            otherSystemInfoVo.setCreateTime(new Date());
            otherSystemInfoVo.setDelFlag(YNEnum.N.toString());
            otherSystemInfoVo.setFlag(FxfzConstants.FXFZ_HDC);
            otherSystemInfoVo.setReleaseColumn("探查类-活断层分布图");
            otherSystemInfoVo.setReleaseDate(new Date());
            otherSystemInfoVo.setType(YNEnum.N.toString());
            otherSystemInfoVo.setPictureFile(attachmentInfoEntity.getAttachId());
            BeanUtils.copyProperties(otherSystemInfoVo,thematicMap);
            theMapService.save(thematicMap);
        }
    }

    @Override
    public void updateAttachVO(UpdateAttachVO updateAttachVO) throws IOException {
        MultipartFile file = updateAttachVO.getFile();
        //获取文件名称
        String fileName = file.getOriginalFilename();
        //获取文件上传的本地路径
        String dateDir = new SimpleDateFormat("/yyyy/MM/dd").format(new Date());
        String savePath = localDir + dateDir;
        //构建文件
        assert fileName != null;
        //上传文件
        String realFileName = uploadFile(fileName, savePath, file);
        //构建实体类
        AttachmentInfoEntity attachmentInfoEntity = buildAttachment(fileName, dateDir, realFileName);
        //保存附件到数据库
        int flag = 0;
        if (ObjectUtils.isNotEmpty(attachmentInfoEntity)) {
            flag = attachmentInfoService.singleSave(attachmentInfoEntity);
        }
        updateAttachVO.setAttachUrl(attachmentInfoEntity.getAttachId());
        hddcMapRepository.updateAttachVO(updateAttachVO);
    }

    @Override
    public void delete(String ids) {
        List<String> split= Arrays.asList(ids.split(","));
        //String join = String.join(",", split);
        hddcMapRepository.delete(split);
    }



    private String uploadFile(String fileName, String savePath,MultipartFile multipartFile) throws IOException {
        // 获取文件的后缀名
        assert fileName != null;
        String preFileName = fileName.substring(0,fileName.lastIndexOf("."));
        // .jpg  .pdf  .txt  .docs
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        // 拼文件名  真实的文件名称
        String realFileName = preFileName + "_"  + System.currentTimeMillis() + fileType;
        //判断文件夹是否存在如果不存在则创建
        if(!new File(savePath).exists()){
            new File(savePath).mkdirs();
        }
        File file = new File(savePath, realFileName);
        multipartFile.transferTo(file);
        return realFileName;
    }

    private AttachmentInfoEntity buildAttachment(String fileName, String dateDir, String realFileName) {
        AttachmentInfoEntity attachmentInfoEntity = new AttachmentInfoEntity();
        attachmentInfoEntity.setAttachId(StringUtils.buildPrimaryId());
        attachmentInfoEntity.setAttachName(fileName);
        attachmentInfoEntity.setAttachPath( dateDir + "/" + realFileName);
        attachmentInfoEntity.setFileDirId(localDir);
        attachmentInfoEntity.setIsValid("1");
        attachmentInfoEntity.setIsCompleted("1");
        return attachmentInfoEntity;
    }
}
